﻿@model MvcXRepair.ViewModel.ChangePasswordViewModel

@{
    ViewBag.Title = "ChangePassword";
}

<h2>Change Password</h2>
@if (ViewBag.Succeeded != null && Convert.ToBoolean(ViewBag.Succeeded))
{
    <p>Your password has been successfully updated. Please use this password at your next login.</p>
}
else
{
    <section id="changePasswordForm">
        @using (Html.BeginForm("ChangePassword", "User", FormMethod.Post))
        {
@*Html.AntiForgeryToken()*@
            @Html.ValidationSummary(true)

            <div id="jsValidationMsg" class="validation-summary-errors">
                <ul></ul>
            </div>

            <fieldset>
                <legend>Log in Form</legend>
                <ol>
                    <li>
                        @Html.LabelFor(m => m.CurrentPassword)
                        @Html.TextBoxFor(m => m.CurrentPassword, new { @type = "password" })
                        @Html.ValidationMessageFor(m => m.CurrentPassword)
                    </li>
                    <li>
                        @Html.LabelFor(m => m.NewPassword)
                        @Html.PasswordFor(m => m.NewPassword)
                        @Html.ValidationMessageFor(m => m.NewPassword)
                    </li>
                </ol>
                <input type="submit" value="Change password" />
            </fieldset>        
        }
    </section>

    @section lazy
{
        <script type="text/javascript">
            $(function () {
                $("#CurrentPassword").blur(function () {
                    var val = $(this).val();

                    if (!val == false) {
                        $.ajax({
                            url: "/User/CheckChangePassword",
                            data: "currentPassword=" + val,
                            type: 'POST',
                            dataType: "json",
                            async: false,
                            success: function (resp) {
                                if (resp == true) {
                                    $("#jsValidationMsg > ul > li.passwordCompareIssue").remove();
                                }
                                else {
                                    $("#jsValidationMsg > ul").append("<li class='passwordCompareIssue'>Current password is not correct.</li>");
                                }
                            }
                        });

                    }
                });
            });
        </script>
    }
}
